import {
    judgeOs,
    arrayMove,
    getCurrAbsPath,
    rltToAbs,
    arrayEqual,
    arrayDiff,
    OlFunction,
    inheritClass,
    Delegate,
    encodeHTML,
    decodeHTML,
    templateStringRender,
    strToVar,
    Hashcaller,
    HashListener,
    hashcaller,
    requestAPI,
    download
} from "../js/basics/Basics.js";
import {
    stopPropagation,
    stopEvent,
    stopPE,
    nodeListToArray,
    KeyNotbook,
    addKeyEvent,
    removeKeyEvent,
    addResizeEvent,
    linkClick,
    setupLinkClick
} from "../js/basics/dom_tool.js";
import {
    getBezierMatrix,
    get_BezierCurvePoint__DeCasteljau,
    matrixMULT,
    calcPascalsTriangle,
    getBezierCoefficient,
    get_BezierDerivativesPoints,
    createCutBezierMatrixQ,
    cutBezierByMatrix,
    binaryLinearEquation,
    approximately,
    derivative,
    rootsOfCubic,
    coefficientToPoints,
    Stepper,
    deg
} from "../js/basics/math_ex.js";
import {
    cueObjToMediaObj,
    DEF_MediaObjMarkList,
    DEF_MediaObjMark,
    DEF_MediaObj,
    DEF_CUEOBJTrack,
    cueTimeToSecond,
    loadCue,
} from "../js/basics/cue_media_obj.js";
import{
    DEF_VirtualElementList,
    DEF_VirtualElement,
    DEF_CSSVE,
    DEF_CSSVEItem,
    DataLink,
    AttrKeyStrCtrl,
    AttrKeyStrCtrl__Ex,
    AttrKeyStrCtrlList,
    CtrlLib,
    ExCtrl
} from "../js/ControlLib/CtrlLib.js"

import {getExCtrl} from "./EXCtrl.js"
import {getPageCtrl} from "./page.js"

setupLinkClick();
var indexjsUrl=getCurrAbsPath();
var pageType=location.hash.split("/")[1];

var leftBox=document.getElementById("Left");
var main={};
var mainBox=document.getElementById("Main");

var pageCtrl,exCtrl;

var indexnav;
var audioControl;
function getEXCtrlCallback(ctrlList){
    exCtrl=ctrlList;
    getPageCtrl(getPageCtrlCallback,exCtrl);
    indexnav=new exCtrl.IndexNav("leftIndex");
    indexnav.data={
        d1List:[
            {
                url:"./index",
                text:"Index",
                title:"Index",
                icon:"index"
                // child:[]
            },{
                url:"./blog",
                text:"blog",
                title:"Darth's web log",
                icon:"bbs",
                child:[
                    {
                        url:"./blog/code",
                        text:"code",
                        title:"code notbook"
                    },{
                        url:"./blog/food",
                        text:"food",
                        title:"eat food"
                    }
                ]
            }
        ]
    };
    indexnav.addend(leftBox);
    
    audioControl=new exCtrl.AudioControl();
    audioControl.addend(leftBox);
    audioControl.addItem(DEF_MediaObj.clone({
        urlList:["/media/audio/music/Touhou Imperishable Night Speed Medley For The Insane Bugs [Arrange].mp3"],
        title:"Touhou Imperishable Night Speed Medley For The Insane Bugs [Arrange]",
        cover:["/media/audio/music/cover.svg"]
    }));
    audioControl.addItem(DEF_MediaObj.f("/media/audio/music/Azis - Gadna poroda.mp3",function(){audioControl.reRender();}));
    audioControl.loadCue("/media/audio/music/1979. Space - Just Blue (2006, Russia, Gala Records - GL 10438) Remastered/Space - Just Blue.cue");
    audioControl.loadCue("/media/audio/music/1982. Paris France Transit - Paris • France • Transit (1997, Russia, PolyGram - 534109-2) Reissue/Didier Marouani & Paris-France-Transit - Paris • France • Transit.cue");
}
getExCtrl(getEXCtrlCallback);

function getPageCtrlCallback(ctrlList){
    pageCtrl=ctrlList;
    renderPage(pageType);
}
/** 添加 hash 渲染界面
 * 必须在控件加载完成后运行
 * 只能运行一次
 */
function renderPage(pageType){
    if(renderPage.c) return;
    renderPage.c=true;
    hashcaller.add(new HashListener(
        /.*/i,
        show404Page
    ))
    hashcaller.add(new HashListener(
        /^index\/?$/i,
        showIndexPage
    ));
    hashcaller.add(new HashListener(
        /^\/?$/i,
        showIndexPage
    ));
    hashcaller.add(new HashListener(
        /^(blog.*?)\/?$/i,
        showSelectPage
    ));
    hashcaller.add(new HashListener(
        /^(blog\/.*?)\/page\d+\/?$/i,
        showSelectPage
    ));
    hashcaller.add(new HashListener(
        /^blog\/(\d+)\/?$/i,
        showBlogPage
    ));
    window.lowhash='';
    hashcaller.touchHashListener();
}
function showIndexPage(){
    // 此处的main是页面中的一个元素
    if(main.contentCtrl){
        //如果原本有加载过控件，先清除。
        main.contentCtrl.removeCtrl();
    }
    // 实例化一个控件
    main.contentCtrl=new pageCtrl.home();
    // 将该控件添加在页面中
    main.contentCtrl.addend(mainBox);
}
function showSelectPage(regex){
    if(!(main.contentCtrl instanceof pageCtrl.select)){
        if(main.contentCtrl) main.contentCtrl.removeCtrl();
        main.contentCtrl=new pageCtrl.select();
        main.contentCtrl.addend(mainBox);
    }else{
        main.contentCtrl.reRender();
    }
}
function showBlogPage(regex){
    if(main.contentCtrl){
        main.contentCtrl.removeCtrl();
    }
    main.contentCtrl=new pageCtrl.blog();
    main.contentCtrl.blogID=parseInt(regex[1]);
    main.contentCtrl.addend(mainBox);
}
function showBBSPage(){
    if(main.contentCtrl){
        main.contentCtrl.removeCtrl();
    }
    main.contentCtrl=new pageCtrl.bbs();
    main.contentCtrl.addend(mainBox);
}
function show404Page(){
    if(main.contentCtrl){
        main.contentCtrl.removeCtrl();
    }
    main.contentCtrl=new pageCtrl.page404();
    main.contentCtrl.addend(mainBox);
}
renderPage.c=false;



hashcaller.touchHashListener();